/*
    This file is part of tPortal.

    tPortal is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    tPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with tPortal.  If not, see <http://www.gnu.org/licenses/>.

    The original code was written by Sebastien Bettinger <sebastien.bettinger@gmail.com>

 */

package net.tinyportal.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tinyportal.persistence.PortletZoneDao;
import net.tinyportal.persistence.entity.Layout;
import net.tinyportal.service.LayoutService;
import net.tinyportal.service.PortletService;
import net.tinyportal.service.PreferenceService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/layout")
public class LayoutEndpoint implements ApplicationContextAware {
	@Autowired
	ApplicationContext applicationContext;

	/**
	 * 
	 */
	private static final long serialVersionUID = 7934374005770615573L;

	/**
	 * Logger
	 */
	private static final Log log = LogFactory.getLog(LayoutEndpoint.class);

	/**
	 * Jsp du portail, définie dans le web.xml
	 */
	private String portalJsp;

	@Autowired
	private PreferenceService preferenceService;

	@Autowired
	private PortletService portletService;

	@Autowired
	private LayoutService layoutService;

	@Autowired
	private PortletZoneDao portletZoneDao;

	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String  LayoutName = request.getParameter("layout");
		Layout layout = null;
		if (LayoutName != null)
			layout = layoutService.getLayout(LayoutName);
		String json = new JSONObject(layout).toString();
		response.getWriter().write(json);
		return null;

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

}
